package com.zbkj.business.kaoshi.reservationrecord;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.annotation.OpenApiTag;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: exam_reservation_record预约记录
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("reservationRecord")
@Slf4j
@OpenApi
//@DS("prod")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@OpenApiTag(value = "预约记录",tag = "${projectName}")
public class ReservationRecordService extends BaseService<ReservationRecordDao, ReservationRecord> implements IService<ReservationRecord> {

    /**
     * list
     */
    @OpenApiTag("查询预约记录列表")
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
//        cdoRequest.pageHandle();
//        List<CDO> pageList = this.baseMapper.getPageList(cdoRequest);
//        int total = this.baseMapper.getPageListCount(cdoRequest);
//        CDO cdoResult=new CDO().set("total",total).set("datalist",pageList);
        return super.getPageList(cdoRequest);
    }
    /**
     * info
     */
    @OpenApiTag("查询预约记录信息")
    public Return info(CDO cdoRequest) {
        ReservationRecord bo = BeanUtil.toBean(cdoRequest, ReservationRecord.class);
        LambdaQueryWrapper<ReservationRecord> lqw = buildQueryWrapper(bo);
        ReservationRecord reservationRecord=super.getOne(lqw);
        return Return.valueOf(reservationRecord);
    }

    @OpenApiTag("根据ID查询预约记录信息")
    public Return queryById(CDO cdoRequest) {
        ReservationRecord reservationRecord=super.getById(cdoRequest.getLongValue("id"));
        return Return.valueOf(reservationRecord);
    }

    private LambdaQueryWrapper<ReservationRecord> buildQueryWrapper(ReservationRecord reservationRecord) {
        LambdaQueryWrapper<ReservationRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ReservationRecord::getId);

        lqw.eq(reservationRecord.getId() != null, ReservationRecord::getId, reservationRecord.getId());
        lqw.like(StringUtils.isNotBlank(reservationRecord.getRecordName()), ReservationRecord::getRecordName, reservationRecord.getRecordName());
        lqw.like(StringUtils.isNotBlank(reservationRecord.getRecordDate()), ReservationRecord::getRecordDate, reservationRecord.getRecordDate());
        lqw.like(StringUtils.isNotBlank(reservationRecord.getCreateBy()), ReservationRecord::getCreateBy, reservationRecord.getCreateBy());
        lqw.eq(reservationRecord.getCreateDate() != null, ReservationRecord::getCreateDate, reservationRecord.getCreateDate());
        lqw.like(StringUtils.isNotBlank(reservationRecord.getUpdateBy()), ReservationRecord::getUpdateBy, reservationRecord.getUpdateBy());
        lqw.eq(reservationRecord.getUpdateDate() != null, ReservationRecord::getUpdateDate, reservationRecord.getUpdateDate());
        lqw.like(StringUtils.isNotBlank(reservationRecord.getRemarks()), ReservationRecord::getRemarks, reservationRecord.getRemarks());
        lqw.eq(reservationRecord.getDelFlag() != null, ReservationRecord::getDelFlag, reservationRecord.getDelFlag());
        return lqw;
    }

    /**
     * save
     */
    @Override
    @OpenApiTag("保存预约记录信息")
    public Return save(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        ReservationRecord bo = BeanUtil.toBean(cdoRequest, ReservationRecord.class);
        Return ret = this.validEntity(cdoRequest,bo,0);
        if(ret.getCode()!=0){
            return ret;
        }
        super.save(bo);
        return Return.SUCCESS;
    }
    /**
     * update
     */
    @OpenApiTag("更新预约记录信息")
    public Return update(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        ReservationRecord bo = BeanUtil.toBean(cdoRequest, ReservationRecord.class);
        Return ret = this.validEntity(cdoRequest,bo,1);
        if(ret.getCode()!=0){
            return ret;
        }
        super.updateById(bo);
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    @OpenApiTag("删除预约记录信息")
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if(b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }




    private Return validEntity(CDO cdoRequest,ReservationRecord entity,Integer type){
        //TODO 做一些数据校验,如唯一约束
        return Return.SUCCESS;
    }
}
